{extend name="public/base" /}
{block name="title"}我的订单{/block}
{block name="css"}
<link rel="stylesheet" type="text/css" href="__JS__/mylayer/mylayer.css">
{/block}
{block name="content"}
<div id="huoList" class="bcf8 height100">
    <ul class="liebiao-ul" data-mark="{$type}">
        <li class="fs13 cl33 {eq name='$type' value='all'}heng-active{/eq}" data-type='all'>
            <div class="fs14 cl33 heng-div"><span class="heng-span">全部</span> </div>
        </li>
        <li class="fs13 cl33 {eq name='$type' value='0'}heng-active{/eq}" data-type='0'>
            <div class="fs14 cl33 heng-div "><span class="heng-span">待付款</span> </div>
        </li>
        <li class="fs13 cl33 {eq name='$type' value='1'}heng-active{/eq}" data-type='1'>
            <div class="fs14 cl33 heng-div "><span class="heng-span">待发货</span> </div>
        </li>
        <li class="fs13 cl33 {eq name='$type' value='2'}heng-active{/eq}" data-type='2'>
            <div class="fs14 cl33 heng-div"><span class="heng-span">待收货</span> </div>
        </li>
        <li class="fs13 cl33 {eq name='$type' value='3'}heng-active{/eq}" data-type='3'>
            <div class="fs14 cl33 heng-div"><span class="heng-span">待评价</span> </div>
        </li>
    </ul>
    <div class="liebiao-content">
        <div id="order-list"></div>
    </div>
</div>
{/block}
{block name="footjs"}
<script type="text/javascript" src="__THIRD__/layer/mobile/layer.js" ></script>
<script type="text/javascript" src="__JS__/mylayer/mylayer.js"></script>
<script type="text/javascript" src="__THIRD__/dropload/dropload.min.js"></script>
<script type="text/javascript">
function orderHtml(data) {
    var html = '';
    $.each(data, function(i, v){
        var sumGds = 0;
        html += '<div class="geren-daifukuan" data-id="'+v[0].order_no+'">'
                    +'<div class="geren-daifukuan-box"><a href="'+'{:route(\'Order/detail\')}?no='+v[0].order_no+'" >';

        $.each(v[1], function(ii, vv){
            sumGds += vv.goods_num
            html +=     '<div class="geren-daifukuan-content tl fs0">'
                            +'<div>'
                                +'<img src="__FILEROOT__/thumb'+vv.goods_img+'" alt />'
                            +'</div>'
                            +'<div class="geren-daifukuan-content-word">'
                                +'<div class="fs14 cl33">'+vv.goods_name+'</div>'
                                +'<div class="fs12 cl66 mt10 over2">'+vv.goods_sku+'</div>'
                            +'</div>'
                            +'<div class="geren-daifukuan-content-pri">'
                                +'<div class="over">'
                                    +'<span class="fs10 cl33">¥</span>'
                                    +'<span class="fs14 cl33">'+vv.unit_price+'</span>'
                                +'</div>'
                                +'<div class="over">'
                                    +'<span class="fs12 cl66">x</span>'
                                    +'<span class="fs12 cl66">'+vv.goods_num+'</span>'
                                +'</div>'
                            +'</div>'
                        +'</div>';
        })

        if(v[0].order_status != 0 && v[0].order_status != 4){
            var price = v[0].pay_money
        }else{
            var price = v[0].price
        }
        console.log(price)
        html +=         '</a><div class="geren-daifukuan-bottom">'
                            +'<div class="fs12 cl33">共'
                                +'<span>'+sumGds+'</span>件商品&nbsp&nbsp&nbsp'
                                +'<span>合计：<span class="fs10 cl33 ls10">¥</span>'
                                +'<span class="fs14 fwb">'+price+'</span>'
                                +'</span>'
                            +'</div>'
                        +'</div>'
                        +'<div class="geren-daifukuan-btn-box fs0">';

        if(v[0].order_status == 0){
            html +=          '<div class="fs12 clog">待付款</div>'
                            +'<div>'
                                +'<button type="button" class="orderOpt" data-o="001">取消订单</button>'
                                +'<button type="button" class="orderOpt" data-o="002">付款</button>';
        }else if(v[0].order_status == 1){
            html +=          '<div class="fs12 clog">待发货</div>'
                            +'<div>'
                                +'<button type="button" class="orderOpt" data-o="101">退款</button>';
        }else if(v[0].order_status == 11){
            html +=          '<div class="fs12 clog">退款中</div>'
                            +'<div>'
                                +'<button type="button" class="orderOpt" data-o="1101">取消退款</button>';
        }else if(v[0].order_status == 2){
            html +=          '<div class="fs12 clog">待收货</div>'
                            +'<div>'
                                +'<button type="button" class="orderOpt" data-o="201">确认收货</button>';
        }else if(v[0].order_status == 3){
            html +=          '<div class="fs12 clog">交易完成</div>'
                            +'<div>'
                                +'<button type="button" class="orderOpt" data-o="401">删除订单</button>'
                                +'<button type="button" class="orderOpt" data-o="301">评价</button>';
        }else if(v[0].order_status == 31){
            html +=          '<div class="fs12 clog">有商品退货中</div>'
                            +'<div>';
        }else if(v[0].order_status == 32){
            html +=          '<div class="fs12 clog">交易完成</div>'
                            +'<div>';
        }else if(v[0].order_status == 4){
            html +=          '<div class="fs12 clog">交易取消</div>'
                            +'<div>'
                                +'<button type="button" class="orderOpt" data-o="401">删除订单</button>';
        }else if(v[0].order_status == 5){
            html +=          '<div class="fs12 clog">退款中</div>'
                            +'<div>';
        }else if(v[0].order_status == 6){
            html +=          '<div class="fs12 clog">已退款</div>'
                            +'<div>';
        }

        html +=             '</div>'
                        +'</div>'
                    +'</div>'
                +'</div>';
    })

    return html;
}

function changeOrderStatus(orderNo, status, callback) {
    if(orderNo == '' || status === ''){
        layer_msg('非法操作')
        return false;
    }

    var idx = layer_loading();
    $.post('{:route(\'User/changeOrderStatus\')}', {order_no: orderNo, status: status}, function(res){
        if(res.code == 1){
            if(typeof callback == 'function'){
                callback()
                layer.close(idx);
            }else{
                window.location.reload();
            }
        }else{
            layer_msg('系统错误')
            layer.close(idx);
        }
    }, 'json')
}
</script>
<script>
$(function() {
    $(".liebiao-ul li").click(function() {
        var type = $(this).data('type')
        var reloadUrl = '{:route(\'order\')}?o='+type
        window.location.replace(reloadUrl)
    })

    var pageStart = 1;
    var perpage = 8;
    
    $('.liebiao-content').dropload({
        scrollArea: window,
        domDown: {
            domRefresh:'<div class="dropload-refresh">↑上拉加载更多</div>',
            domLoad: '<div class="dropload-load">加载中</div>',
            domNoData: '<div class="dropload-noData">没有更多订单</div>'
        },
        loadDownFn: function(me) {
            // 拼接HTML
            var result = '';
            $.ajax({
                type: 'POST',
                url: '{:route("Order/orderList")}',
                dataType: 'json',
                data:{perpage: perpage, pageStart: pageStart, type: $('.liebiao-ul').data('mark')},
                success: function(res) {
                    if(res.code == 1){
                        var data = res.result
                        var dataLen = data.length
                        var orders = orderHtml(data)
                        // 插入数据到页面，放到最后面
                        $('#order-list').append(orders);

                        pageStart += 1;

                        //数据条数小于每页显示的最大数
                        if(dataLen < perpage){
                            me.lock(); // 锁定
                            me.noData(); // 无数据
                        }
                    }else{
                        // 锁定
                        me.lock();
                        // 无数据
                        me.noData();
                    }

                    // 每次数据插入，必须重置
                    me.resetload();
                },
                error: function(xhr, type) {
                    // 即使加载出错，也得重置
                    me.resetload();
                    return false;
                }
            });
        },
        threshold: 50
    });

    $('body').on('click', '.orderOpt', function(){
        var _this = $(this)
        var order_no = _this.parents('.geren-daifukuan').data('id')
        var opt = _this.data('o')
        
        if(opt == '001'){
            layer_confirm('取消订单', function(){
                changeOrderStatus(order_no, 4)
            }, ['是', '否'])
        }else if(opt == '002'){
            window.location.href = '{:route(\'Order/detail\')}?from=list&no='+order_no;
        }else if(opt == '101'){
            layer_confirm('确认退款？<br/><span style="font-size:.24rem;">申请退款后请耐心等待，若有疑问请联系客服。</span>', function(idx){
                layer.close(idx);
                changeOrderStatus(order_no, 11)
            })
        }else if(opt == '1101'){
            layer_confirm('取消退款', function(idx){
                layer.close(idx);
                changeOrderStatus(order_no, 12)
            }, ['是', '否'])
        }else if(opt == '201'){
            layer_confirm('确认收货', function(idx){
                layer.close(idx);
                changeOrderStatus(order_no, 3)
            })
        }else if(opt == '301'){
            window.location.href = '{:route(\'User/reviews\')}?no='+order_no;
        }else if(opt == '401'){
            layer_confirm('删除订单', function(idx){
                layer.close(idx);
                changeOrderStatus(order_no, -1, function(){
                    _this.parents('.geren-daifukuan').remove()
                })
            })
        }
    })
})
</script>
{/block}